Skip to content

Added embedding API to C#, JS, Python and Rust SDKs#581

Open
phanindraraja wants to merge 11 commits intomicrosoft:mainfrom
phanindraraja:user/rchava/embedding-support
Open

Added embedding API to C#, JS, Python and Rust SDKs#581
phanindraraja wants to merge 11 commits intomicrosoft:mainfrom
phanindraraja:user/rchava/embedding-support

Conversation

@phanindraraja
Copy link
Copy Markdown
Member

@phanindraraja phanindraraja commented Apr 3, 2026

Summary

Adds embedding support to all four Foundry Local SDKs (C#, JavaScript, Python, Rust), enabling text embedding generation through the OpenAIEmbeddingClient via the FoundryLocalCore native interop layer. Supports both single and batch input.

Changes

C# SDK

  • OpenAIEmbeddingClient — Client with GenerateEmbeddingAsync(string) for single input and GenerateEmbeddingsAsync(IEnumerable<string>) for batch. Configurable via EmbeddingSettings (Dimensions, EncodingFormat).
  • EmbeddingRequestResponseTypes.cs — Request DTO extending Betalgo's EmbeddingCreateRequest with FromUserInput() factory for both single and batch. Response deserialization with null-check and error handling.
  • IModel.GetEmbeddingClientAsync() — New interface method, implemented in Model and ModelVariant.
  • JsonSerializationContext — Registered EmbeddingCreateRequestExtended and EmbeddingCreateResponse for AOT.

JavaScript SDK

  • EmbeddingClient — Client with generateEmbedding(string) and generateEmbeddings(string[]). Settings with encodingFormat validation. Shared executeRequest() for both paths.
  • IModel.createEmbeddingClient() — Factory method in interface, Model, and ModelVariant.
  • Exported from index.ts.

Python SDK

  • EmbeddingClient — Client with generate_embedding(str) and generate_embeddings(List[str]). Uses OpenAI SDK types (EmbeddingCreateParams for request, CreateEmbeddingResponse for response). Settings with encoding_format validation via _serialize(). Patches server response to add missing object and usage fields required by the OpenAI SDK type.
  • IModel.get_embedding_client() — Abstract method, implemented in Model and ModelVariant.
  • Exported from openai/__init__.py.

Rust SDK

  • EmbeddingClient — Client with generate_embedding(&str) and generate_embeddings(&[&str]). Uses async_openai::types::embeddings::CreateEmbeddingResponse as return type. Patches server response for missing object and usage fields. Settings with encoding_format validation. Builder pattern for dimensions() and encoding_format().
  • Model.create_embedding_client() — Factory method in Model and ModelVariant.
  • Added "embedding-types" feature to async-openai dependency.

Tests

All SDKs include tests for:

  • Basic embedding generation (1024 dimensions, correct response structure)
  • L2 normalization (norm ≈ 1.0, values within [-1, 1])
  • Different inputs produce different embeddings (cosine similarity < 0.99)
  • Same input produces identical embeddings (determinism)
  • Batch embedding (multiple inputs → multiple results with correct indices)
  • Batch results match single-input results
  • Input validation (empty input, empty list)
  • Known golden values (C# only)

Documentation

  • All four SDK READMEs updated with embeddings feature, usage examples, and settings documentation.
  • C# API docs (index.md, imodel.md, model.md, modelvariant.md, openaiembeddingclient.md).
  • JS docs (README.md class index).
  • Rust API docs (docs/api.md with EmbeddingClient, EmbeddingResponse, EmbeddingData reference).
  • Python API reference table updated.

Test plan

  • C# SDK tests pass
  • Python SDK tests pass
  • JS SDK tests pass
  • Rust SDK tests pass
  • All existing chat/audio tests unaffected

Dependencies

This PR depends on the FoundryLocalCore (neutron-server) PR that adds the "embeddings" NativeInterop command, /v1/embeddings endpoint, and batch support:
https://microsoft.visualstudio.com/windows.ai.toolkit/_git/neutron-server/pullrequest/15212502

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 3, 2026

Someone is attempting to deploy a commit to the MSFT-AIP Team on Vercel.

A member of the Team first needs to authorize it.

@phanindraraja phanindraraja changed the title Added embedding API to SDK Added embedding API to C# SDK Apr 3, 2026
@phanindraraja phanindraraja marked this pull request as ready for review April 3, 2026 07:03
Copilot AI review requested due to automatic review settings April 3, 2026 07:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an embeddings capability to the Foundry Local C# SDK, aligning it with the existing OpenAI-compatible chat and audio clients and exposing it through the model abstraction.

Changes:

  • Introduces OpenAIEmbeddingClient with request/response serialization helpers and JSON source-gen registrations.
  • Exposes embeddings via IModel.GetEmbeddingClientAsync() and implements it on Model / ModelVariant with loaded-model enforcement.
  • Adds test coverage plus README/API docs updates for the new embeddings surface.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
sdk/cs/src/OpenAI/EmbeddingClient.cs Adds OpenAIEmbeddingClient and the public embeddings API surface.
sdk/cs/src/OpenAI/EmbeddingRequestResponseTypes.cs Adds embeddings request DTO + response parsing/serialization helpers.
sdk/cs/src/IModel.cs Adds GetEmbeddingClientAsync to the public model interface.
sdk/cs/src/Detail/Model.cs Wires GetEmbeddingClientAsync through to the selected variant.
sdk/cs/src/Detail/ModelVariant.cs Implements GetEmbeddingClientAsync with “must be loaded” validation.
sdk/cs/src/Detail/JsonSerializationContext.cs Registers embeddings request/response types for STJ source generation.
sdk/cs/test/FoundryLocal.Tests/EmbeddingClientTests.cs Adds embeddings integration tests (basic, normalization, determinism, known values).
sdk/cs/README.md Documents embeddings feature and shows usage + settings.
sdk/cs/docs/api/microsoft.ai.foundry.local.openaiembeddingclient.md Adds generated API docs for OpenAIEmbeddingClient.
sdk/cs/docs/api/microsoft.ai.foundry.local.imodel.md Updates generated API docs for IModel with embeddings accessor.
sdk/cs/docs/api/microsoft.ai.foundry.local.model.md Updates generated API docs for Model with embeddings accessor.
sdk/cs/docs/api/microsoft.ai.foundry.local.modelvariant.md Updates generated API docs for ModelVariant with embeddings accessor.
sdk/cs/docs/api/index.md Adds OpenAIEmbeddingClient to the API docs index.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@phanindraraja phanindraraja force-pushed the user/rchava/embedding-support branch 2 times, most recently from cbba093 to 2789034 Compare April 8, 2026 23:16
Raja Phanindra Chava and others added 10 commits April 10, 2026 05:38
Added null checks

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* updated js sdk

* updated python sdk

* updated rust sdk

* Updated embedding model test aliases

* Updated ordering on readme

---------

Co-authored-by: Raja Phanindra Chava <rchava@microsoft.com>
@phanindraraja phanindraraja force-pushed the user/rchava/embedding-support branch from 2ddc1a2 to 23d58e4 Compare April 10, 2026 12:38
…s as input (#3)

Co-authored-by: Raja Phanindra Chava <rchava@microsoft.com>
@phanindraraja phanindraraja changed the title Added embedding API to C# SDK Added embedding API to C#, JS, Python and Rust SDKs Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants